import java.util.*;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class ClusterBase {
    private JedisCluster jc = null;
    private List<String> ipList;

    public long initRedisCluster(String ips, String password) {
        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();

        //Jedis Cluster will attempt to discover cluster nodes automatically

        String[] iplist = ips.split("#");
        // 高可靠部署
        ipList = new ArrayList<String>();
        for (int i = 0; i < iplist.length; i++) {
            ipList.add(iplist[i]);
            String[] tmpString = iplist[i].split(":");
            if (tmpString.length < 1) {
                System.err.println("Input IP:Port Error!");
            } else {

                jedisClusterNodes.add(new HostAndPort(tmpString[0], Integer
                        .parseInt(tmpString[1])));
                System.out.println("ADD IP:" + tmpString[0] + " Port:" + tmpString[1]);
            }
        }

        JedisPoolConfig conf = new JedisPoolConfig();
        conf.setMaxTotal(5000);
        conf.setMaxWaitMillis(10000);

        conf.setBlockWhenExhausted(true);
        conf.setTestOnBorrow(true);

        jc = new JedisCluster(jedisClusterNodes, 1500, 1000);

        Map<String, JedisPool> nodes = jc.getClusterNodes();
        jc.set("1", "2");
        System.err.println("Init JedisCluster Size:" + nodes.size());

        return 1;
    }

    public JedisCluster getJedisCluster() {
        return jc;
    }

    public void closeJedisCluster() {
        jc.close();
    }
}
